﻿using GameLogic.Data;
using GameProtocol;
using Google.Protobuf;
using LitJson;
using TEngine;
using UnityEngine;

namespace GameLogic
{
    public class ShipDataContainer
    {
        GameProtocol.ShipData m_Data;
        ShipDesignData m_ShiDesignData;
        GameConfig.ships.Ship m_ShipCfg;

        public int CfgID { get => m_Data.CfgID; }
        public int InstanceID { get => m_Data.InstanceID; }
        public int StuctPoint { get => m_Data.Sp; set => m_Data.Sp = value; }
        public int StuctPointMax { get => m_ShipCfg.StructPoint; }
        public int ElectricityPoint { get => m_Data.Ep; set => m_Data.Ep = value; }
        public int ElectricityPointMax { get => m_ShipCfg.ReserveElectricity; }
        public int FightVelocityMax { get => m_ShipCfg.FightVelocityMax; }
        public int LinearAcceleration { get => m_ShipCfg.Acceleration; }
        public int AngularAcceleration { get => m_ShipCfg.AngularAcceleration; }
        public int AngularVelocityMax { get => m_ShipCfg.AngularVelocityMax; }

        public string SpritePath { get => m_ShipCfg.SpritePath; }
        public ShipData Data { get => m_Data; set => m_Data = value; }
        public ShipDesignData ShiDesignData { get => m_ShiDesignData; set => m_ShiDesignData = value; }

        ShipDataContainer()
        {

        }
        public static ShipDataContainer Create(ShipData shipData)
        {
            ShipDataContainer ship = new ShipDataContainer();
            ship.m_Data = shipData;
            ship.Init(ship.CfgID);
            return ship;

        }
        public static ShipDataContainer Create(int shipID, int instanceID)
        {
            ShipDataContainer ship = new ShipDataContainer();
            ship.Init(shipID);
            ship.m_Data = new ShipData();
            ship.m_Data.CfgID = shipID;
            ship.m_Data.InstanceID = instanceID;
            return ship;
        }

        public void Init(int shipID)
        {
            m_ShipCfg = ConfigSystem.Instance.Tables.TbShips.GetOrDefault(shipID);
            if (m_ShipCfg == null)
            {
                Debug.LogError("找不到舰船的静态配置文件：" + shipID);
                return;
            }
            
            m_ShiDesignData = ShipModule.Instance.GetShipDesign(shipID);
            if (m_ShiDesignData == null)
            {
                Debug.LogError("找不到舰船的设计文件：" + m_ShipCfg.DesignPath);
                return;
            }
            m_Data.Sp = m_ShipCfg.StructPoint;
        }

        public byte[] Serialize()
        {
            return m_Data.ToByteArray();
        }
        public void Unserialize(byte[] data)
        {
            //m_Data = GameProtocol.Record.ShipData.Parser.ParseFrom(data);
        }
        static public int UnparseShipCfgID(GameProtocol.ShipData root)
        {
            return 10000;
        }
    }
}
